require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['zepto','common/mcommon'],function($,com){
        com.init();
		var pageLogic = new PageLogic($,com);
		pageLogic.init();

	});
});
var PageLogic = function($,com){
	return{
		init:function(){
			var _this = this;
			_this.type = 1;
			_this.loadStateData();
			_this.bindEvents();
		},
		bindEvents:function(){
			var _this = this;
			$("#selProvince").on("change",function(){
				_this.selectCity($(this).val());
				if($(this).val() != "0"){
					_this.loadSysData({
						type:_this.type,
						prov:$(this).val()
					});
				}else{
					_this.clearContent();
				}
			});
			$("#selCity").on("change",function(){
				_this.selectZone($("#selProvince").val(), $(this).val());
				if($(this).val() !="0"){
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
						city:$("#selCity").val()
					});
				}else{
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
					});
				}
			});
			$("#selArea").on("change",function(){
				if($(this).val() !="0"){
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
						city:$("#selCity").val(),
						area:$("#selArea").val()
					});
				}else{
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
						city:$("#selCity").val()
					});
				}
				
			});
			$("#btnTxt").on("click",function(){
				_this.loadSysData({
					type:_this.type,
					prov:$("#selProvince").val(),
					city:$("#selCity").val(),
					area:$("#selArea").val(),
					name:$("#potName").val()
				});
			});
		},
		loadStateData:function(){
			var _this = this;
			com.ajax({
				url:'/prov.json',
				data:{type:1},
				type:'get',
				success:function(res){
					if(res.success){
						_this.selectProvince(res.data);
					}
				}
			}); 
		},
		selectProvince:function(provs){
			var _this = this;
			$("#selProvince").html(_this.mkOption(provs,"省","prov"));
			$("#selCity").html(_this.mkOption(null,"市","city"));
			$("#selArea").html(_this.mkOption(null,"区","area"));
			if(_this.prov){
				$("#selProvince").val(_this.prov);
				$("#selProvince").trigger("change");
			}
		},
		selectCity:function(code){
			var _this = this;
			if(code == "0"){
				$("#selCity").html(_this.mkOption(null,"市","city"));
				$("#selArea").html(_this.mkOption(null,"区","area"));
				return;
			}
			com.ajax({
				url:'/city.json',
				data:{type:_this.type,prov:code},
				type:'get',
				success:function(res){
					if(res.success){
						var data = res.data;
						$("#selCity").html(_this.mkOption(data,"市","city"));
						$("#selArea").html(_this.mkOption(null,"区","area"));
						if(_this.city && !_this.cityInit){
							$("#selCity").val(_this.city);
							$("#selCity").trigger("change");
							_this.cityInit = 1;
						}
					}
				}
			}); 
		},
		selectZone:function(pCode,citycode){
			var _this = this;
			if(citycode == "0"){
				$("#selArea").html(_this.mkOption(null,"区","area","area"));
				return;
			}
			com.ajax({
				url:'/area.json',
				data:{type:_this.type,prov:pCode,city:citycode},
				type:'get',
				success:function(res){
					if(res.success){
						var data = res.data;
						$("#selArea").html(_this.mkOption(data,"区","area"));
						if(_this.area && !_this.areaInit){
							$("#selArea").val(_this.area);
							$("#selArea").trigger("change")
							_this.areaInit = 1;
						} 
					}
				}
			});
			
		},
		mkOption:function(data,name,col){
			var options = '<option value="0">' + name + '</option>';
			if(data){
				for(var key in data){
					var partNm = col + "Name";
					options += ' <option value="' +  data[key][col] + '">' + data[key][partNm] + '</option>'
				}
			}
			return options;
		},
		loadSysData:function(obj){
			var _this = this;
			var data = {};
			for(var key in obj){
				if(obj[key] && obj[key]!="0"){
					data[key] = obj[key];
				}
			}
			var _this = this;
			com.ajax({
				url:"/shopQuery.json",
				data:data,
				success:function(res){
					if(res.data){
						_this.genContent(res.data);
					}
				}
			})
		},
		clearContent:function(){
			$(".items").html("");
		},
		genContent:function(data){
			var html = "";
			$(data).each(function(){
				html += '<li> ';
				html += '	<div class="item-title"> '+ this.name;
				html += '	</div> ';
				html += '	<div class="item-detail"> ';
				html += '		<span class="item-detail-con">' + this.detail;
				html += '	</div> ';
				html += '</li> ';
			});
			$(".items").html(html);
		}
	}
}